#!/usr/bin/env python
import sys
sys.path.insert(0, '../')

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings")


import django
django.setup()


import os

from apps.goods.models import SKU
from meiduo_mall import settings
from utils.goods import get_categories, get_breadcrumb, get_goods_specs


def generic_detail_html(sku):

    # try:
    #     sku = SKU.objects.get(id=sku_id)
    # except SKU.DoesNotExist:
    #     return JsonResponse({'code': 400, 'errmsg': '参数缺失'})

    categories = get_categories()
    breadcrumb = get_breadcrumb(sku.category)
    goods_specs = get_goods_specs(sku)

    context = {
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs': goods_specs,
    }

    from django.template import loader
    detail_tamplate = loader.get_template('detail.html')
    detail_html_data = detail_tamplate.render(context)

    file_path = os.path.join(os.path.dirname(settings.BASE_DIR),'front_end_pc/goods/%s.html'%sku.id)
    with open('file_path','w',encoding='utf-8') as f:
        f.write(detail_html_data)

    print(sku.id)

skus=SKU.objects.all()
for sku in skus:
    generic_detail_html(sku)